package at.htlkaindorf.jsf.view;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;

@ManagedBean(name = "language")
@SessionScoped
public class LanguageBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private String localeCode;
    private Locale currLocale;

    private static Map<String, Object> countries;

    static {
        countries = new LinkedHashMap<String, Object>();
        countries.put("Deutsch", Locale.GERMAN); // label, value
        countries.put("English", Locale.ENGLISH);
    }
    {
        currLocale = Locale.GERMAN;
        localeCode = currLocale.toString();
    }

    public Map<String, Object> getCountriesInMap() {
        return countries;
    }


    public String getLocaleCode() {
        return localeCode;
    }


    public void setLocaleCode(String localeCode) {
        this.localeCode = localeCode;
    }


    public Locale getCurrLocale() {
        return currLocale;
    }


    public void setCurrLocale(Locale currLocale) {
        this.currLocale = currLocale;
    }


    // value change event listener
    public void countryLocaleCodeChanged(ValueChangeEvent e) {
        String newLocaleValue = e.getNewValue().toString();
        // loop country map to compare the locale code
        for (Map.Entry<String, Object> entry : countries.entrySet()) {
            if (entry.getValue().toString().equals(newLocaleValue)) {
                currLocale = (Locale)entry.getValue();
                localeCode = currLocale.toString();
            }
        }
    }

}
